package com.market.plus.core.dao.jdbc.rowmapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.market.plus.core.domain.Avatar;
import com.market.plus.core.domain.Comment;
import com.market.plus.core.domain.ContentFile;
import com.market.plus.core.domain.News;
import com.market.plus.core.domain.Participant;

public class NewsRowMapper implements RowMapper<News>{

   public News mapRow(ResultSet resultSet, int rownumber) throws SQLException {
      ContentFile contentFile = new ContentFile();
      contentFile.setId(resultSet.getLong("c_contentfile_id"));
      contentFile.setFileName(resultSet.getString("c_filename"));
      contentFile.setGuid(resultSet.getString("c_guid"));

      Avatar avatar = new Avatar();
      avatar.setId(resultSet.getLong("a_avatar_id"));
      avatar.setContentFile(contentFile);

      Participant participant = new Participant();
      participant.setId(resultSet.getLong("p_participant_id"));
      participant.setFirstName(resultSet.getString("p_frist_name"));
      participant.setLastName(resultSet.getString("p_last_name"));
      participant.setNickname(resultSet.getString("p_nickname"));
      participant.setGuid(resultSet.getString("p_participant_guid"));
      participant.setAvatar(avatar);

      News news = new News();
      news.setId(resultSet.getLong("n_news_id"));
      news.setContent(resultSet.getString("n_content"));
      news.setGuid(resultSet.getString("n_guid"));
      news.setTitle(resultSet.getString("n_title"));
      news.setModifyedUser(resultSet.getString("n_modifyed_user"));
      news.setModifyedDate(resultSet.getDate("n_modifyed_date"));
      news.setTotalComments(resultSet.getInt("n_totalcomments"));
      news.setParticipant(participant);

      Long comment_id = resultSet.getLong("co_id");
      if (comment_id != null && comment_id.longValue() > 0){
         ContentFile commentContentFile = new ContentFile();
         commentContentFile.setId(resultSet.getLong("co_contentfile_id"));
         commentContentFile.setFileName(resultSet.getString("co_filename"));
         commentContentFile.setGuid(resultSet.getString("co_guid"));

         Avatar commentAvatar = new Avatar();
         commentAvatar.setId(resultSet.getLong("co_avatar_id"));
         commentAvatar.setContentFile(commentContentFile);

         Participant commentParticipant = new Participant();
         commentParticipant.setId(resultSet.getLong("co_participant_id"));
         commentParticipant.setFirstName(resultSet.getString("co_frist_name"));
         commentParticipant.setLastName(resultSet.getString("co_last_name"));
         commentParticipant.setNickname(resultSet.getString("co_nickname"));
         commentParticipant.setGuid(resultSet.getString("co_participant_guid"));
         commentParticipant.setAvatar(commentAvatar);

         Comment comment = new Comment();
         comment.setId(comment_id);
         comment.setContent(resultSet.getString("co_content"));
         comment.setModifyedDate(resultSet.getDate("co_modifyed_date"));
         comment.setParticipant(commentParticipant);
         List<Comment> comments = new ArrayList<Comment>();
         comments.add(comment);
         news.setComments(comments);
      }
      return news;
   }
}
